<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>店员管理</title>
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-lg" lay-event="add">添加店员</button>
  </div>
  <div class="demoTable">
      搜索名字：
      <div class="layui-inline">
          <input type="text" id="searchName" lay-verify="required" autocomplete="off" placeholder="" class="layui-input" >
      </div>
      <button class="layui-btn" data-type="reload" lay-event="search">搜索</button>
  </div>


</script>


<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="../../layui/layui.js" charset="utf-8"></script>

<script>
  layui.use('table', function(){
    var table = layui.table
            ,$ = layui.jquery;

    // table的渲染过程
    var tableIns =  table.render({
      elem: '#test'
      ,url:'http://localhost:8080/clerk/list'
      ,headers:{type:"getList"}
      ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
      ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
        title: '提示'
        ,layEvent: 'LAYTABLE_TIPS'
        ,icon: 'layui-icon-tips'
      }]
      ,title: '店员数据表'
      ,cols: [[
        {field:'id', title:'编号', width:200, fixed: 'left', unresize: true, sort: true}
        ,{field:'name', title:'名字', width:300}
        ,{field:'sex', title:'性别', width:200}
        ,{field:'telephone', title:'联系方式', width:300}
        ,{field:'position', title:'职位', width:100}
        ,{field:'salary', title:'薪资', width:150,sort: true}
        ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:300}
      ]]
      ,page: true
    });
    //头工具栏事件
    table.on('toolbar(test)', function(obj){
      switch(obj.event){
        case 'add':
          // 添加店员的操作
          layer.open({
            title: '添加店员'
            ,type:2
            ,content: 'add.html'
            ,area: ['45%', '85%']
            ,end:function(){
              location.reload();
            }
          });
          break;
        case 'search':
            let searchName=document.getElementById("searchName").value;
            tableIns =  table.render({//.reload({

                elem: '#test'
                ,url:'http://localhost:8080/clerk/search?searchName='+searchName
                ,headers:{type:"getList"}
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '会员数据表'
                ,cols: [[
                    {field:'id', title:'编号', width:200, fixed: 'left', unresize: true, sort: true}
                    ,{field:'name', title:'名字', width:300}
                    ,{field:'sex', title:'性别', width:200}
                    ,{field:'telephone', title:'联系方式', width:300}
                    ,{field:'position', title:'职位', width:100}
                    ,{field:'salary', title:'薪资', width:150,sort: true}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:300}
                ]]
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            break;

      }
    });


    //监听行工具事件
    table.on('tool(test)', function(obj){
      // data:当前选中行的全字段json数据
      var data = obj.data;
      console.log(data)
      if(obj.event === 'edit'){

        // 打开一个弹层 layer ,把cpnEdit.html页面显示出来
        layer.open({
          title: '编辑'
          ,type:2
          ,content: 'update.html'
          ,area: ['45%', '90%']
          ,success: function(layero, index){   // 弹层打开时的回调操作
            // 获取cpnDetail页面中的根元素
            var body = layer.getChildFrame('body', index);

            // 找到对应的元素, 把值填充到对应的位置
            body.find('input[name="name"]').val(data.name)
            //设置修改携带的id
            body.find('input[name="id"]').val(data.id)
            body.find('input[name="sex"]').val(data.sex)
            body.find('input[name="telephone"]').val(data.telephone)
            body.find('input[name="position"]').val(data.position)
            body.find('input[name="salary"]').val(data.salary)
            body.find('img[name="clerkImage"]').prop('src',data.img)
            body.find('img[name="clerkImage"]').prop('layer-src',data.img)
            body.find('input[name="image"]').prop('value',data.img)
          }
        });
      } else if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
          $.ajax({
            url: "/clerk/delete",
            data:{
              id:obj.data['id']
            },
            success:function(result){
                layer.msg(result.msg,{icon:1,time:1000},function(){
                  tableIns.reload();
                });
            }
          })
        });
      }
    });
  });
</script>
</body>
</html>